function _classCallCheck(e, t) {
  if (!(e instanceof t))
    throw new TypeError("Cannot call a class as a function");
}

Object.defineProperty(exports, "__esModule", { value: !0 });
var _extends =
    Object.assign ||
    function (e) {
      for (var t = 1; t < arguments.length; t++) {
        var n = arguments[t];
        for (var r in n)
          Object.prototype.hasOwnProperty.call(n, r) && (e[r] = n[r]);
      }
      return e;
    },
  _createClass = (function () {
    function e(e, t) {
      for (var n = 0; n < t.length; n++) {
        var r = t[n];
        (r.enumerable = r.enumerable || !1),
          (r.configurable = !0),
          "value" in r && (r.writable = !0),
          Object.defineProperty(e, r.key, r);
      }
    }
    return function (t, n, r) {
      return n && e(t.prototype, n), r && e(t, r), t;
    };
  })(),
  Request = (function () {
    function e() {
      _classCallCheck(this, e),
        (this.config = { baseUrl: "", business: "data" }),
        (this.interceptor = {
          request: void 0,
          response: void 0,
          fail: void 0,
        }),
        (this._success = function (e, t, n, r, o) {
          if (n.statusCode >= 200 && n.statusCode <= 401) {
            var s = n.data;
            "file" === t.contentType &&
              "string" == typeof s &&
              (void 0 === t.dataType || "json" === t.dataType) &&
              (s = JSON.parse(n.data));
            var i = t.skipInterceptorResponse;
            if (
              (e.interceptor.response &&
                "function" == typeof e.interceptor.response &&
                !i &&
                (s = e.interceptor.response(s, t)),
              i || s.success)
            ) {
              var a = t.business ? s[t.business] : s;
              return (
                t.debug && console.log("response success: ", a),
                void (t.success ? t.success(a) : r(a))
              );
            }
          }
          e._fail(e, t, n, r, o);
        }),
        (this._fail = function (e, t, n, r, o) {
          if (
            (t.debug && console.error("response failure: ", n),
            "request:fail abort" !== n.errMsg)
          ) {
            var s = n;
            e.interceptor.fail &&
              "function" == typeof e.interceptor.fail &&
              (s = e.interceptor.fail(n, t)),
              t.fail ? t.fail(s) : o(s);
          }
        }),
        (this._prepare = function (e, t) {
          ((arguments.length > 2 && void 0 !== arguments[2]
            ? arguments[2]
            : {}
          ).startTime = Date.now()),
            t.loadingTip && uni.showLoading({ title: t.loadingTip }),
            "file" === t.contentType &&
              ((void 0 !== t.formData && null !== t.formData) ||
                ((t.formData = t.data), delete t.data),
              delete t.header["Content-Type"],
              delete t.header.Referer,
              (t.method = "POST")),
            t.debug && console.log("request: ", t);
        }),
        (this._complete = function (e, t, n) {
          var r =
            arguments.length > 3 && void 0 !== arguments[3] ? arguments[3] : {};
          if (
            ((r.endTime = Date.now()),
            t.debug &&
              console.log("请求用时 " + (r.endTime - r.startTime) + " 毫秒"),
            t.loadingTip)
          ) {
            var o = r.endTime - r.startTime,
              s = t.loadingDuration || 500;
            (o = o < s ? s - o : 0),
              setTimeout(function () {
                uni.hideLoading();
              }, o);
          }
        });
    }
    return (
      _createClass(
        e,
        [
          {
            key: "setConfig",
            value: function (e) {
              this.config = Object.assign(this.config, e);
            },
          },
          {
            key: "request",
            value: function () {
              var t =
                  arguments.length > 0 && void 0 !== arguments[0]
                    ? arguments[0]
                    : {},
                n = this;
              void 0 === t.data && (t.data = {}),
                void 0 === t.header && (t.header = {});
              var r = Object.assign({}, this.config, t);
              (r = Object.assign(t, r)),
                (r.url = e.getUrl(r)),
                r.header["Content-Type"] ||
                  (r.header["Content-Type"] = e.getContentType(r));
              var o = r;
              n.interceptor.request &&
                "function" == typeof n.interceptor.request &&
                (o = n.interceptor.request(r));
              var s = void 0,
                i = new Promise(function (e, t) {
                  var r = {};
                  n._prepare(n, o, r),
                    "file" === o.contentType
                      ? ((s = uni.uploadFile(
                          _extends({}, o, {
                            success: function (r) {
                              n._success(n, o, r, e, t);
                            },
                            fail: function (r) {
                              n._fail(n, o, r, e, t);
                            },
                            complete: function (e) {
                              n._complete(n, o, e, r);
                            },
                          })
                        )),
                        o.progress &&
                          "function" == typeof o.progress &&
                          s.onProgressUpdate(function (e) {
                            o.progress(e, s);
                          }))
                      : (s = uni.request(
                          _extends({}, o, {
                            timeout: 6e5,
                            success: function (r) {
                              n._success(n, o, r, e, t);
                            },
                            fail: function (r) {
                              n._fail(n, o, r, e, t);
                            },
                            complete: function (e) {
                              n._complete(n, o, e, r);
                            },
                          })
                        ));
                });
              return o.success || o.fail || o.complete ? s : i;
            },
          },
          {
            key: "get",
            value: function () {
              var e =
                arguments.length > 0 && void 0 !== arguments[0]
                  ? arguments[0]
                  : {};
              return (e.method = "GET"), this.request(e);
            },
          },
          {
            key: "post",
            value: function () {
              var e =
                arguments.length > 0 && void 0 !== arguments[0]
                  ? arguments[0]
                  : {};
              return (e.method = "POST"), this.request(e);
            },
          },
          {
            key: "upload",
            value: function () {
              var e =
                arguments.length > 0 && void 0 !== arguments[0]
                  ? arguments[0]
                  : {};
              return (
                (e.method = "POST"), (e.contentType = "file"), this.request(e)
              );
            },
          },
        ],
        [
          {
            key: "posUrl",
            value: function (e) {
              return /(http|https):\/\/([\w.]+\/?)\S*/.test(e);
            },
          },
          {
            key: "getUrl",
            value: function (t) {
              var n = t.url || "",
                r = e.posUrl(n);
              if (!r) {
                t.slashAbsoluteUrl && (r = /^\/([\w.]+\/?)\S*/.test(n));
              }
              return r ? n : t.baseUrl + n;
            },
          },
          {
            key: "getContentType",
            value: function (e) {
              var t = e.contentType || "json",
                n = e.encoding || "UTF-8";
              if ("json" === t) return "application/json;charset=" + n;
              if ("form" === t)
                return "application/x-www-form-urlencoded;charset=" + n;
              if ("file" === t) return "multipart/form-data;charset=" + n;
              if ("text" === t) return "text/plain;charset=" + n;
              if ("html" === t) return "text/html;charset=" + n;
              throw new Error("unsupported content type : " + t);
            },
          },
        ]
      ),
      e
    );
  })(),
  request = new Request();

exports.default = request;
